package com.example.jikeyoujikeyou.office_sz.model.base;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeFilter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;

public class BaseModel implements Serializable{

    public BaseModel() {
    }

    /**
     * key - 属性名
     * value - 属性值
     */
    private HashMap<String,Object> attrs = new HashMap<>();

    /**
     * 构造方法.
     * @param json
     */
    public BaseModel(String json) {
        try {
            JSONObject object = JSONObject.parseObject(json);
            Iterator it = object.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                attrs.put(key,object.get(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 向attrs种添加值.
     * @param key
     * @param value
     */
    public void put(String key,Object value){
        attrs.put(key,value);
    }

    /**
     * 向attrs获取值.
     * @param key
     */
    public Object get(String key){
        if (!attrs.containsKey(key)) {
            return null;
        }
        return attrs.get(key);
    }

    /**
     * 向attrs添加一个model.
     * @param model
     */
    public void putModel(BaseModel model){
        attrs.putAll(model.attrs);
    }

    /**
     * 获取attrs.
     * @return HashMap
     */
    public HashMap<String, Object> getAttrs() {
        return attrs;
    }

    /**
     * 设置map.
     * @param attrs
     */
    public void putAttrs(HashMap<String, Object> attrs) {
        this.attrs = attrs;
    }
}